package com.example.admin.controller;

import com.example.admin.service.AdUserService;
import com.usian.model.admin.dtos.UserDto;
import com.usian.model.admin.pojos.AdUser;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Api(tags = "用户相关接口")
public class AdController {

    @Autowired
    private AdUserService adUserService;

    @PostMapping("/user/v1/add_user")
    @ApiOperation(value = "新增用户接口", notes = "新增用户接口")
    public ResponseResult addUser(@RequestBody UserDto userDto) {
        if (userDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Integer integer = adUserService.addUser(userDto);
        if (integer > 0){
            return ResponseResult.okResult(200, "成功");
        }
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);

    }
    @GetMapping("/user/v1/get_user_by_name")
    @ApiOperation(value = "根据名字查询用户接口", notes = "根据名字查询用户接口")
    public ResponseResult getUser(@RequestParam String name) {
        //int i = 1/0;
        if (name == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        List<AdUser> userList = adUserService.getUserByName(name);
        if (CollectionUtils.isEmpty(userList)){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return PageResponseResult.okResult(userList);

    }

}
